package com.tianzeculture.stagebao.activity.business;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.ImageBean;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.net.BusinessRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/6/27 0027.
 * 招募详情
 */

public class RecruitDetailActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.ivPublish)
    CircleImageView ivPublish;
    @BindView(R.id.tagItems)
    TagFlowLayout tagItems;

    @BindView(R.id.tvPublisher)
    TextView tvPublisher;
    @BindView(R.id.tvPublishTime)
    TextView tvPublishTime;
    //    @BindView(R.id.tvCheckPublisher)
//    TextView tvCheckPublisher;
    @BindView(R.id.tvJobTitle)
    TextView tvJobTitle;
    @BindView(R.id.tvDingwei)
    TextView tvDingwei;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvPerson)
    TextView tvPerson;
    @BindView(R.id.tvSex)
    TextView tvSex;
    @BindView(R.id.tvHeight)
    TextView tvHeight;
    @BindView(R.id.tvBaoxiao)
    TextView tvBaoxiao;
    @BindView(R.id.tvFuzhuang)
    TextView tvFuzhuang;
    @BindView(R.id.tvEntered)
    TextView tvEntered;
    @BindView(R.id.tvPrice)
    TextView tvPrice;
    @BindView(R.id.tvJobContent)
    TextView tvJobContent;
    @BindView(R.id.btnSure)
    Button btnSure;
    @BindView(R.id.ibtnDelete)
    ImageButton ibtnDelete;

    NoticeList notice = null;
    String workId;
    private int deleteNoteceId = 55;

    @Request
    BusinessRestUsage businessRestUsage = null;

    @Override
    public void initUI() {
        super.initUI();
        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);
        tvTitle.setText("招募详情");
        ibtnDelete.setVisibility(View.VISIBLE);
        notice = (NoticeList) getIntent().getSerializableExtra("recruitDetail");
        workId = notice.getId();

        initData();
    }

//    @OnClick(R.id.tvCheckPublisher)
//    public void toSeller() {
//        //前往商家详情页
//        Intent intent = new Intent(this, BusinessActivity.class);
//        intent.putExtra("userId", notice.getAddPeopleId());
//        startActivity(intent);
//    }

    @OnClick(R.id.btnSure)
    public void sure() {
        startActivity(new Intent(this, PublishNoticeActivity.class).putExtra("isRecruit", true).putExtra("recruitDetail", notice));
    }

    @OnClick(R.id.ibtnDelete)
    public void deleteNotice() {
        businessRestUsage.deleteNotice(deleteNoteceId, workId);
    }


    private void initData() {

        Glide.with(this).load(Util.IMAGE_URL + notice.getImgurl())
                .placeholder(R.mipmap.icon_head)
                .error(R.mipmap.icon_head)
                .into(ivPublish);
        tvPublisher.setText(notice.getAddPeople());
        tvPublishTime.setText(notice.getAddTime());
        tvJobTitle.setText(notice.getTitle());
        tvJobContent.setText(notice.getContent());
        tvDingwei.setText(notice.getAddress());
        tvTime.setText(
                notice.getStarttime().substring(0, 10) + " ~ " +
                        notice.getEndtime().substring(0, 10));
        tvPerson.setText("人数:" + notice.getPeople() + "人");
        String sex = "";
        if (notice.getSex().equals("0"))
            sex = "女";
        else if (notice.getSex().equals("1"))
            sex = "男";
        else
            sex = "不限";
        tvSex.setText("性别:" + sex);
        tvHeight.setText("身高:" + notice.getHeight());
        if (notice.getIstravel().equals("1"))
            tvBaoxiao.setVisibility(View.VISIBLE);
        else
            tvBaoxiao.setVisibility(View.GONE);
        if (notice.getIscostume().equals("1"))
            tvFuzhuang.setVisibility(View.VISIBLE);
        else
            tvFuzhuang.setVisibility(View.GONE);
        tvEntered.setText("已报名 " + notice.getCntjob() + " 人");
//        if ("0".equals(notice.getPriceType()))
        tvPrice.setText("¥" + notice.getUnitPrice() + "/人/小时");
//        else
//            tvPrice.setText("¥" + notice.getUnitPrice() + "/人/天");

        List<String> strings = new ArrayList<>();
//        if (notice.getTag() == null)
//            strings.add("");
//        else {
//            if (notice.getTag().contains(",")) {
//                String[] s = notice.getTag().split(",");
//                for (int i = 0; i < s.length; i++) {
//                    strings.add(s[i]);
//                }
//            } else {
//                strings.add(notice.getTag());
//            }
//        }
        if (!"".equals(notice.getTag()) && notice.getTag() != null) {
            if (notice.getTag().contains(",")) {
                String[] s = notice.getTag().split(",");
                for (int k = 0; k < s.length; k++) {
                    strings.add(s[k]);
                }
            } else {
                strings.add(notice.getTag());
            }
        }
        tagItems.setAdapter(new TagAdapter<String>(strings) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                View view = LayoutInflater.from(RecruitDetailActivity.this).inflate(R.layout.item_characteristic,
                        tagItems, false);
                TextView tvCharacteristic = view.findViewById(R.id.tvCharacteristic);
                tvCharacteristic.setText(s);
                if (position % 4 == 0)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpink_round);
                else if (position % 4 == 1)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpurple_round);
                else if (position % 4 == 2)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightorange_round);
                else if (position % 4 == 3)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightgreen_round);
                return view;
            }
        });
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == deleteNoteceId) {
            if (msg.getData()) {
                finish();
            }
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recruit_detailed;
    }
}
